package imooc_math.sort;

import javax.swing.plaf.synth.SynthStyle;

import resource.Student;

//选择排序
public class SelectionSort {
	//sort方法的形参类型要能接受各种类型的实参，并且最终要做比较
	public static Object[] sort(Comparable [] arr){
		int n=arr.length;
		for(int i=0;i<n;i++){
			//寻找[i，n)区间里的最小值/拿到该最小值的索引，与索引为i的值交换
			int minindex=i;
			for(int j=i+1;j<n;j++){
				if(arr[j].compareTo(arr[minindex])<0)
					minindex=j;
			}
			swap(arr,i,minindex);
		}
		return arr;
	}
	public static void swap(Object [] arr,int a,int b){
		Object t=arr[a];
		arr[a]=arr[b];
		arr[b]=t;
	}
	//测试用途
	public static void main(String[] args) {
		Integer[] arr={10,9,8,7,6,5,4,3,2,1};
		SelectionSort.sort(arr);
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i]+"	");
		}
		System.out.println();
		
		String[] s={"a","n","c","m","d"};
		SelectionSort.sort(s);
		for(int i=0;i<s.length;i++){
			System.out.print(s[i]+"	");
		}
		System.out.println();
		
		Double[] d={2.14,0.35,9.23,4.56};
		SelectionSort.sort(d);
		for(int i=0;i<d.length;i++){
			System.out.print(d[i]+"	");
		}
		System.out.println();
		
		Student s1=new Student("a",51);
		Student s2=new Student("b",36);
		Student s3=new Student("c",25);
		Student s4=new Student("d",36);
		Student[] student={s1,s4,s3,s2};
		SelectionSort.sort(student);
		for(int i=0;i<student.length;i++){
			System.out.println(student[i]+"	");
		}
		
	}
}
